package shiyan2;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class shiyan2_4 {
    public static void main(String[] args) throws IOException {
        ServerSocket Server = new ServerSocket(8888);
        while (true){
            Socket sock = Server.accept();
            Thread T = new Handler2(sock);
            T.start();
        }
    }
}


//线程
class Handler2 extends Thread{
    //用于接受和发送的socket
    Socket sock;
    public Handler2(Socket sock){
        this.sock = sock;
    }

    @Override
    public void run(){
        //建立输入输出的buffer
        try(InputStream input = this.sock.getInputStream()){
            try(OutputStream output = this.sock.getOutputStream()) {
                var writer = new BufferedWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8));
                var reader = new BufferedReader(new InputStreamReader(input,StandardCharsets.UTF_8));
                while (true){
                    String username = reader.readLine();
                    String pwd = reader.readLine();
                    if(username.equals("admin")&&pwd.equals("admin")){
                        writer.write("登陆成功"+"\n");
                        writer.flush();
                    }else {
                        writer.write("登陆失败"+"\n");
                        writer.flush();
                    }
                }
            }
        }catch (Exception e){
            try {
                this.sock.close();
            }catch (IOException ioe) {
            }
        }
    }
}
